-- @owner:yangjian
-- @date:2024-4-6
-- @testpoint:1.去除大对象使用限制，测试loread函数的参数表现正常，合理报错
 
--清理环境
CREATE OR REPLACE FUNCTION f_opengauss_large_object_function_case0007_1()  
RETURNS VOID AS 
$$
BEGIN  
    BEGIN
    PERFORM lo_unlink(100);
    EXCEPTION
        WHEN others THEN
            NULL;
    end;
END;  
$$
LANGUAGE plpgsql;
/

select f_opengauss_large_object_function_case0007_1();

--环境准备
drop table if exists t_opengauss_large_object_function_case0007_2 cascade;
CREATE TABLE t_opengauss_large_object_function_case0007_2 (a int);
insert into t_opengauss_large_object_function_case0007_2 values (1);
insert into t_opengauss_large_object_function_case0007_2 values (2);

--step1:创建表存储函大对象信息;expect:成功
drop table if exists t_opengauss_large_object_function_case0007_1 cascade;
CREATE TABLE t_opengauss_large_object_function_case0007_1 (loid oid,fd int);

--step2:测试fd参数为存在值;expect:成功
INSERT INTO t_opengauss_large_object_function_case0007_1 (loid) SELECT lo_create(100);
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 6);
end;
/

--step3:测试fd参数为不存在值;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (1, 6);
end;
/
end;
--step4:测试fd参数为非int类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread ('a', 6);
end;
/
end;

--step5:测试len参数为小于实际字符数;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 9);
end;
/

--step6:测试len参数为大于实际字符数;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 11);
end;
/

--step7:测试len参数为等于实际字符数;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 10);
end;
/

--step8:测试len参数为非int类型;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 'a');
end;
/

--step9:测试len参数为负数;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, -1);
end;
/

--step10:测试len参数超int4;expect:失败
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, 2147483648);
end;
/
end;
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, -2147483649);
end;
/
end;
--step11:测试len参数为空值;expect:成功
begin;
SELECT lo_open(100,CAST(x'20000' | x'40000' AS integer));
select lowrite (0,'aaaaaaaaaa');
select loread (0, null);
end;
/

--清理环境
select lo_unlink(loid) from t_opengauss_large_object_function_case0007_1;
select f_opengauss_large_object_function_case0007_1();
drop table if exists t_opengauss_large_object_function_case0007_1 cascade;
drop table if exists t_opengauss_large_object_function_case0007_2 cascade;
drop function f_opengauss_large_object_function_case0007_1();
